package com.ubs.training.server.aop;

import com.ubs.training.prod.server.service.LoggingService;
import com.ubs.training.server.dao.model.Rating;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/24/12
 * Time: 5:26 AM
 * To change this template use File | Settings | File Templates.
 */
@Aspect
@Component
public class LoggingRatingAdvice {

    @Autowired
    private LoggingService lsvc;

    @After("execution(* com.ubs.training.server.service.RatingService.*(..))")
    public void aopLoggerServiceMethod(JoinPoint thisJoinPoint) throws Throwable {
        String methodName = thisJoinPoint.getSignature().getName();
        Object[] methodArgs = thisJoinPoint.getArgs();

        if(methodName.equals("saveRating"))
        {
            Rating rating = (Rating) methodArgs[0];
            if(rating.getRatingID()==0)
            {
                lsvc.logEvent("AOP!!! New rating was saved!!! to client with id == " + rating.getClientID());
            }
            else
            {
                lsvc.logEvent("AOP!!! Rating was updated!!! to client with id == " + rating.getClientID());
            }

        }
        else if(methodName.equals("deleteRating"))
        {
            Rating rating = (Rating) methodArgs[0];
            lsvc.logEvent("AOP!!! Rating was deleted!!! the id if rating was == " + rating.getRatingID());
        }
        else if(methodName.equals("getAllRatingsByID"))
        {
            int position=-1;
            if(methodArgs[0] instanceof Integer)
            {
                position = (Integer)methodArgs[0] ;
            }
            lsvc.logEvent("AOP!!! return all ratings by client id = " +position);
        }
        else if(methodName.equals("getById"))
        {
            int position=-1;
            if(methodArgs[0] instanceof Integer)
            {
                position = (Integer)methodArgs[0] ;
            }
            lsvc.logEvent("AOP!!! return rating by id = " + position);
        }


    }

    public LoggingService getLsvc() {
        return lsvc;
    }

    public void setLsvc(LoggingService lsvc) {
        this.lsvc = lsvc;
    }
}
